/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package amfservices;

import flex.messaging.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author quocpt
 */
public class ClientContext
{

    private static ThreadLocal<ClientApp> clientApp;
    private static ThreadLocal<HashMap<String, Object>> customData;

    public static void createThreadLocalObjects()
    {
	if (clientApp == null)
	{
	    customData = new ThreadLocal<HashMap<String, Object>>(){}; // do no override initialValue() - avoid risk memory leak
	    clientApp = new ThreadLocal<ClientApp>(){};
	}
    }

    public static void startLocalObject()
    {
	clientApp.set(new ClientApp());
	customData.set(new HashMap<String, Object>());
    }

    public static void endLocalObject()
    {
	clientApp.set(null);
	customData.set(null);
    }

    public static HttpServletRequest getHttpServletRequest()
    {
	return FlexContext.getHttpRequest();
    }

    public static HttpServletResponse getHttpServletResponse()
    {
	return FlexContext.getHttpResponse();
    }

    public static ServletConfig getAMFServletConfig()
    {
	return FlexContext.getServletConfig();
    }

    public static ClientApp getClientApp()
    {
	return clientApp.get();
    }

    public static <T extends Object> void setData(String key, T data)
    {
	HashMap<String, Object> hm = customData.get();
	hm.put(key, data);
    }

    @SuppressWarnings("unchecked")
    public static <T extends Object> T getData(String key)
    {
	HashMap<String, Object> hm = customData.get();
	return (T) hm.get(key);
    }
}